package com.ruoyi.common.enums;

import lombok.Getter;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/** 通用枚举
 * @author LiuXiaoYu
 * @date 2021/7/19- 9:42
 */
@Getter
public enum ExceptionEnum {
    /**
     * 通用枚举结果
     */
    ERROR(500,"出现错误请联系管理员"),
    REQUEST_PATH(404,"请求路径错误"),
    SUCCESS(200,"成功"),
    NEED_LOGIN(10001,"需要登录"),
    PERMISSION_DENY(10002,"权限不足"),
    LOGIN_FAIL(10003,"登录失败"),
    REPEAT_SUBMIT(10004,"重复提交表单,请稍后再试"),
    NO_SET_DATASCOPE(10005,"未设置数据权限"),
    ERROR_VALID_ARGUMENTS(10006,"参数验证错误");
    private final Integer code;
    private final String desc;


    ExceptionEnum(Integer code, String desc){
        this.code=code;
        this.desc=desc;
    }

    private static final Map<Integer, ExceptionEnum> CACHE =new HashMap<>();

    static{
        for (ExceptionEnum exceptionEnum: ExceptionEnum.values()){
            CACHE .put(exceptionEnum.code,exceptionEnum);
        }
    }

     public static String getDesc(Integer code){
        return Optional.ofNullable(CACHE .get(code))
                .map(ExceptionEnum::getDesc)
                .orElseThrow(()-> new IllegalArgumentException("invalid exception code!"));
     }
}
